package coupon

import (
	"context"
	"strings"

	"mall/common/globalkey"
	"mall/common/utils/pointy"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/promotion/model"
	"mall/service/promotion/rpc/internal/svc"
	"mall/service/promotion/rpc/internal/utils/errorhandler"
	"mall/service/promotion/rpc/types/promotion"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type BackendGetCouponProductListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendGetCouponProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendGetCouponProductListLogic {
	return &BackendGetCouponProductListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BackendGetCouponProductListLogic) BackendGetCouponProductList(in *promotion.CouponProductListReq) (*promotion.CouponProductListResp, error) {
	if in.CouponId == nil || *in.CouponId == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	var orderBys []string
	if len(in.OrderBy) > 0 {
		for _, orderBy := range in.OrderBy {
			order := " ASC"
			if orderBy.Order == "1" {
				order = " DESC"
			}
			if orderBy.Field == "id" {
				orderBys = append(orderBys, model.CouponProductRelationshipColumns.RelationshipID+order)
			}
			if orderBy.Field == "sort" {
				orderBys = append(orderBys, model.CouponProductRelationshipColumns.Sort+order)
			}
		}
	}

	filterMap := make(map[string]map[string][]any)
	filterMap[model.CouponProductRelationshipColumns.CouponID] = map[string][]any{
		"equalTo": {*in.CouponId},
	}
	if in.CategoryId != nil && *in.CategoryId > 0 {
		filterMap[model.CouponProductRelationshipColumns.CategoryID] = map[string][]any{
			"equalTo": {*in.CategoryId},
		}
	} else if in.BrandId != nil && *in.BrandId > 0 {
		filterMap[model.CouponProductRelationshipColumns.BrandID] = map[string][]any{
			"equalTo": {*in.BrandId},
		}
	} else if in.ProductId != nil && *in.ProductId > 0 {
		filterMap[model.CouponProductRelationshipColumns.ProductID] = map[string][]any{
			"equalTo": {*in.ProductId},
		}
		if in.ProductType != nil && *in.ProductType > 0 {
			filterMap[model.CouponProductRelationshipColumns.ProductType] = map[string][]any{
				"equalTo": {*in.ProductType},
			}
		}
	}

	var orderBy string
	if len(orderBys) > 0 {
		orderBy = strings.Join(orderBys, ",")
	} else {
		orderBy = model.CouponProductRelationshipColumns.Sort //按序号升序排序，序号小的在前
	}

	res, count, err := l.svcCtx.CouponTemplate.GetRelProductList(l.ctx, model.M{
		"page":     in.Page,
		"pageSize": in.PageSize,
		"orderBy":  orderBy,
	}, 1, filterMap)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	resp := &promotion.CouponProductListResp{
		Total: uint64(count),
	}
	if len(res) > 0 {
		for _, v := range res {
			var sort *uint32
			if v.Sort != uint16(globalkey.SystemDefaultSort) {
				sort = pointy.GetPointer(uint32(v.Sort))
			}
			resp.List = append(resp.List, &promotion.CouponTemplateProductInfo{
				Id:          &v.RelationshipID,
				CouponId:    &v.CouponID,
				BrandId:     &v.BrandID,
				CategoryId:  &v.CategoryID,
				ProductId:   &v.ProductID,
				ProductType: pointy.GetPointer(uint32(v.ProductType)),
				Sort:        sort,
			})
		}
	}

	return resp, nil
}
